const path = require("path");
const VueLoaderPlugin = require("vue-loader/lib/plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const webpack = require("webpack");
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
module.exports = {
  entry: './src/main.js',
  output: {
    filename:'bundle.js',
    path:path.join(__dirname,'dist'),
    publicPath:'dist/'
  },
  resolve: {
    alias: {
      vue$: "vue/dist/vue.esm.js", //配置别名 确保webpack可以找到.vue文件
    },
    extensions: [".js", ".jsx", ".json"],
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        use: "vue-loader",
      },
      {
        test: /\.css$/, // 处理css文件
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.less$/,
        use: ["style-loader", "css-loader", "less-loader"],
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          esModule: false,
            name: 'assets/[name]-[hash].[ext]',
        }
      },
      {
        test: /\.(gif|jpg|jpeg|png|svg)$/, // 处理图片文件
        use: [
          {
            loader: "url-loader",
            options: {
              esModule: false,
              limit: 1024,
              name: "assets/[name]-[hash].[ext]",
            },
          },
        ],
      },
      {
        test: /\.js$/,
        loader: "babel-loader",
      },
    ],
  },
  plugins: [
    // new CopyWebpackPlugin(
    //   {
    //     patterns:[
    //         {
    //           from: path.resolve(__dirname, './src/assets'),  
    //           to: path.resolve(__dirname, './dist/assets'), 
    //         }
    //     ],
    //     options:{
    //       concurrency: 100,
    //     }
    // }),
    new CleanWebpackPlugin(),//实例化，参数为目录
    new VueLoaderPlugin(),
    new HtmlWebpackPlugin({
      filename: `index.html`,
      template: "index.html",
    }),
  ],
};
